package com.whfc.ms.api.mach.service.impl;

import com.whfc.common.exception.BizException;
import com.whfc.common.file.FileHandler;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.RandomUtil;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.service.SysDeptService;
import com.whfc.hdc.dto.AppDeviceDTO;
import com.whfc.hdc.service.AppDeviceService;
import com.whfc.mach.MachOilAddDel;
import com.whfc.mach.dto.AppMachOilRecordDTO;
import com.whfc.mach.entity.AppDeviceObdLog;
import com.whfc.mach.param.*;
import com.whfc.mach.service.AppMachService;
import com.whfc.mach.service.ObdDataService;
import com.whfc.ms.api.mach.service.MsMachService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author sun_guodong
 * @description
 * @date 2020-11-11
 */
@Service
public class MsMachServiceImpl implements MsMachService {

    @DubboReference(interfaceClass = AppMachService.class, version = "1.0.0")
    private AppMachService appMachService;

    @DubboReference(interfaceClass = SysDeptService.class, version = "1.0.0")
    private SysDeptService sysDeptService;

    @DubboReference(interfaceClass = AppDeviceService.class, version = "1.0.0")
    private AppDeviceService commonDeviceService;

    @DubboReference(interfaceClass = ObdDataService.class, version = "1.0.0")
    private ObdDataService obdLogService;

    /**
     * 设备图片地址
     */
    private String machImgPath = "mach/img";

    @Autowired
    private FileHandler fileHandler;

    @Autowired
    private FilePathConfig filePathConfig;

    @Override
    public void add(MachAddParam param) {
        appMachService.add(param);
    }

    @Override
    public void edit(MachEditParam param) {
        appMachService.edit(param);
    }

    @Override
    public void editMachTank(MachTankEditParam param) {
        appMachService.editMachTank(param);
    }

    @Override
    public OssPathDTO export(Integer deptId) {
        List<Integer> deptIds = sysDeptService.getDescendantDeptIdList(deptId);
        String path = appMachService.export(deptIds);
        OssPathDTO ossPathDTO = new OssPathDTO();
        ossPathDTO.setPath(path);
        return ossPathDTO;
    }

    @Override
    public OssPathDTO exportObd(Integer deptId) {
        List<Integer> deptIds = sysDeptService.getDescendantDeptIdList(deptId);
        String path = appMachService.exportObd(deptIds);
        OssPathDTO ossPathDTO = new OssPathDTO();
        ossPathDTO.setPath(path);
        return ossPathDTO;
    }

    @Override
    public void enter(Integer machId) {
        MachEnterParam param = new MachEnterParam();
        param.setMachId(machId);
        param.setTime(new Date());
        appMachService.enter(param);
    }

    @Override
    public void outer(Integer machId) {
        MachEnterParam param = new MachEnterParam();
        param.setMachId(machId);
        param.setTime(new Date());
        appMachService.outer(param);
    }

    @Override
    public void del(Integer machId) {
        appMachService.del(machId);
    }

    @Override
    public void bind(SysUser user, DeviceMachBindParam request) {
        AppDeviceDTO deviceDTO = commonDeviceService.getDeviceBySn(request.getSn());
        if (deviceDTO == null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "设备sn码错误");
        }
        request.setDeviceId(deviceDTO.getId());
        request.setNickName(user.getNickname());
        request.setUserId(user.getId());
        request.setDeviceType(deviceDTO.getType());
        appMachService.bindMachDevice(request);
    }

    @Override
    public void unbind(SysUser user, Integer machId) {
        appMachService.unbindMachDevice(user.getId(), user.getNickname(), machId);
    }

    @Override
    public OssPathDTO uploadImg(MultipartFile file) throws IOException {
        String fileName = RandomUtil.getRandomFileName() + ".png";
        String ossKey = filePathConfig.getFilePath(machImgPath, fileName);
        String upload = fileHandler.upload(ossKey, file.getInputStream());
        OssPathDTO data = new OssPathDTO();
        data.setPath(upload);
        return data;
    }

    @Override
    public void setOilAdd(MachSetOilAddParam param) {
        appMachService.setOilAdd(param);
    }

    @Override
    public ListData<MachOilAdd> getOilAdd(Integer machId, Date date) {
        return appMachService.getOilAdd(machId, date);
    }

    @Override
    public PageData<AppMachOilRecordDTO> getOilAddRecord(Integer deptId, Date startDate, Date endDate, String keyword, Integer pageNum, Integer pageSize) {
        return appMachService.getOilRecordList(deptId, startDate, endDate, keyword, pageNum, pageSize);
    }

    @Override
    public PageData<AppMachOilRecordDTO> getOilAddRecord(Integer machId, Date startDate, Date endDate, Integer pageNum, Integer pageSize) {
        return appMachService.getOilRecordList(machId, startDate, endDate, pageNum, pageSize);
    }

    @Override
    public void addOilManual(MachOilAddManual param) {
        appMachService.addOilManual(param);
    }

    @Override
    public void addOilAdjust(MachOilAddAdjust param) {
        appMachService.addOilAdjust(param);
    }

    @Override
    public void delOilRecord(MachOilAddDel param) {
        appMachService.delOilRecord(param);
    }

    @Override
    public void addObdData(AppDeviceObdLog appDeviceObdLog) {
        obdLogService.innerAddObdData(appDeviceObdLog);
    }

    @Override
    public void sendCmd(MachCmdParam machCmdParam) {
        appMachService.sendMachCmd(machCmdParam);
    }
}
